import axios from 'axios'
import { baseURL } from "@/const"

const baseurl = baseURL

axios.defaults.timeout = 5000

const service = axios.create({
  baseURL: baseurl,
  timeout: 10000, 
  headers: {
    'Content-Type': 'application/json',
    'Access-Control-Allow-Origin': '*',
    // 'Authorization': window.sessionStorage.token ? window.sessionStorage.token : ''
  }
})

// 请求
service.interceptors.request.use(
  config => {
    let token = window.sessionStorage.getItem('token')
    if (token) {
        config.headers['Authorization'] = token;
    }
    return config
  },
  error => {
    console.log(error) 
    return Promise.reject(error)
  }
)

// 响应
service.interceptors.response.use(
  response => {
    const res = response.data

    if (res.code !== '') {
      return res
    } else {
      return res
    }
  },
  error => {
    if (!error.message.includes('timeout')) {
      // Message({
      //   message: '服务器暂无响应，请稍后重试',
      //   type: 'success',
      //   duration: 5 * 1000
      // })
    }
    return Promise.reject(error)
  }
)

export default service
